package com.yonyou.cyxdms.basedata.service.controller.auth;

import com.yonyou.cyxdms.basedata.service.constant.BasedataCommonConstants;
import com.yonyou.cyxdms.basedata.service.service.auth.UserAuthService;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 主子系统认证
 *
 * @author renwd
 * @since 2019/11/26
 */
@Api(tags = "认证相关接口")
@ApiIgnore
@RestController
public class AuthController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserAuthService userAuthService;

    /**
     * 主子系统认证
     *
     * @param paramMap 参数
     * @param headMap header
     * @return com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto
     * @author renwd
     * @since 2019/11/26
     */
    @GetMapping(value = "/auth")
    public LoginInfoDto getUserInfoByUserCode(Map<String, String> paramMap,
                                              @RequestHeader Map<String, String> headMap) {
        String appId = headMap.get(BasedataCommonConstants.APP_ID);
        String jwt = headMap.get(BasedataCommonConstants.TOKEN_NAME);
        if (appId == null) {
            appId = paramMap.get(BasedataCommonConstants.APP_ID);
        }
        if (appId == null) {
            appId = headMap.get(BasedataCommonConstants.APP_ID.toLowerCase());
        }
        if (jwt == null) {
            jwt = paramMap.get(BasedataCommonConstants.TOKEN_NAME);
        }
        logger.debug("token:{},appId:{}", jwt, appId);
        return userAuthService.getLoginInfoDto(appId, jwt);
    }

}
